version 17.0
clear all
set more off
cd "P:\2018\186"
*Figure A2: IRP estimates sensitivity to zeros
use *idnr *earn* *yob *obsy* *max* year woman if year>=1985 using "use\swepanel_100usd", replace
g agec1=year-yob-40
g agef1=fobsy_c-fyob
g agem1=mobsy_c-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
*Sons on average parental income
global inc "learn_c"
global incp "lfmearn_c"
global agep "fmage"
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
preserve
keep if e(sample)==1
keep idnr
duplicates drop idnr, force
compress
sort idnr
save "temp\samp_a2s", replace
restore
*Daughters on average parental income
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
preserve
keep if e(sample)==1
keep idnr
duplicates drop idnr, force
compress
sort idnr
save "temp\samp_a2d", replace
restore
*Sons and daughters pooled on fathers' income
global inc "learn_c"
global incp "lfearn_c"
global agep "agef"
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
preserve
keep if e(sample)==1
keep idnr
duplicates drop idnr, force
compress
sort idnr
save "temp\samp_a2f", replace
restore
*Sons and daughters pooled on mothers' income
global inc "learn_c"
global incp "lmearn_c"
global agep "agem"
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
keep if e(sample)==1
keep idnr
duplicates drop idnr, force
compress
sort idnr
save "temp\samp_a2m", replace
use *idnr *earn* *yob *obsy *max* year woman if year>=1985 using "use\swepanel_zeros", replace
g agec1=year-yob-40
g agef1=fobsy-fyob
g agem1=mobsy-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
merge m:1 idnr using "temp\samp_a2s", gen(samp_a2s)
merge m:1 idnr using "temp\samp_a2d", gen(samp_a2d)
merge m:1 idnr using "temp\samp_a2f", gen(samp_a2f)
merge m:1 idnr using "temp\samp_a2m", gen(samp_a2m)
keep if samp_a2s==3 | samp_a2d==3 | samp_a2f==3 | samp_a2m==3
*Sons on average parental income
global fig "a2s"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if samp_a2s==3, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on average parental income
global fig "a2d"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if samp_a2d==3, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on fathers' income
global fig "a2f"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if samp_a2f==3, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on mothers' income
global fig "a2m"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if samp_a2m==3, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
set obs 35
g year=_n+1984
local est "a2s a2d a2f a2m"
foreach e of local est{
	svmat b`e'
	svmat se`e'
	svmat var`e'
	g sd`e'u=b`e'1+1.96*se`e'1
	g sd`e'l=b`e'1-1.96*se`e'1
}
rename (ba2s1 ba2d1 ba2f1 ba2m1 sda2su sda2du sda2fu sda2mu sda2sl sda2dl sda2fl sda2ml) (roba2s roba2d roba2f roba2m sda2su sda2du sda2fu sda2mu sda2sl sda2dl sda2fl sda2ml)
*Adding IRP estimates
merge 1:1 year using "use\fig1-4_swe", keep(3) keepusing(irp2s sd2su sd2sl irp2d sd2du sd2dl irp3f sd3fu sd3fl irp3m sd3mu sd3ml) nogen
lab var roba2s "son/parental average"
lab var roba2d "daughter/parental average"
lab var roba2f "pooled/father"
lab var roba2m "pooled/mother"
lab var sda2su "son/parental average"
lab var sda2du "daughter/parental average"
lab var sda2fu "pooled/father"
lab var sda2mu "pooled/mother"
lab var sda2sl "son/parental average"
lab var sda2dl "daughter/parental average"
lab var sda2fl "pooled/father"
lab var sda2ml "pooled/mother"
compress
save "use\figa2_swe", replace
*Figure A2a: sons on combined parental income
twoway (connect roba2s year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda2su sda2sl year, lc(ebblue)) (connect irp2s year, ms(d) lc(blue) mc(blue) lp(solid)) (rspike sd2su sd2sl year, lc(blue)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "IRP using IGE sample") label(3 "IRP")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a2a", replace)
graph export "graphs\us_swe\a2a.pdf", replace
graph export "graphs\us_swe\a2a.eps", replace
*Figure A2b: daughters on combined parental income
twoway (connect roba2d year, ms(o) lc(cranberry) mc(cranberry)) (rspike sda2du sda2dl year, lc(cranberry)) (connect irp2d year, ms(d) lc(red) mc(red) lp(solid)) (rspike sd2du sd2dl year, lc(red)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "IRP using IGE sample") label(3 "IRP")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a2b", replace)
graph export "graphs\us_swe\a2b.pdf", replace
graph export "graphs\us_swe\a2b.eps", replace
*Figure A2c: sons and daughters pooled on fathers' income
twoway (connect roba2f year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda2fu sda2fl year, lc(ebblue)) (connect irp3f year, ms(d) lc(blue) mc(blue)) (rspike sd3fu sd3fl year, lc(blue)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "IRP using IGE sample") label(3 "IRP")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a2c", replace)
graph export "graphs\us_swe\a2c.pdf", replace
graph export "graphs\us_swe\a2c.eps", replace
*Figure A2d: sons and daughters pooled on mothers' income
twoway (connect roba2m year, ms(o) lc(cranberry) mc(cranberry)) (rspike sda2mu sda2ml year, lc(cranberry)) (connect irp3m year, ms(d) lc(red) mc(red)) (rspike sd3mu sd3ml year, lc(red)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "IRP using IGE sample") label(3 "IRP")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a2d", replace)
graph export "graphs\us_swe\a2d.pdf", replace
graph export "graphs\us_swe\a2d.eps", replace
******************************************************************************************
*Figure A5: Main specification for prime-aged children aged 33-43
use if yob>=1952 & yob<=1994 & age>=33 & age<=43 & earn!=. using "use\incpanel2", replace
*1) Adding parents
sort idnr
merge m:1 idnr using "use\multigen", keepusing(midnr fidnr myob fyob bcountry mbcountry fbcountry) keep(1 3) nogen
*2) Adding parental earnings and employment 
*mothers
rename (idnr midnr earn emp)(temp1 idnr temp2 temp3)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(mearn mnearn mobsy memp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace mearn=earn if yob==19`i'
	replace mnearn=nearn if yob==19`i'
	replace mobsy=obsy if yob==19`i'
	replace memp=emp if yob==19`i'
	drop earn nearn obsy emp
}
*fathers
rename (idnr fidnr)(midnr idnr)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(fearn fnearn fobsy femp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace fearn=earn if yob==19`i'
	replace fnearn=nearn if yob==19`i'
	replace fobsy=obsy if yob==19`i'
	replace femp=emp if yob==19`i'
	drop earn nearn obsy emp
}
rename (idnr temp1 temp2 temp3)(fidnr idnr earn emp)
mvencode fnearn mnearn, mv(0)
lab var fearn "father's earnings"
lab var mearn "mother's earnings"
lab var fnearn "observations father's earnings"
lab var mnearn "observations mother's earnings"
lab var fobsy "year father's earnings observed"
lab var mobsy "year mother's earnings observed"
lab var femp "father's employment"
lab var memp "mother's employment"
*3) Adding education
rename(idnr fidnr)(temp idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr midnr)(fedlev fedu fidnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr temp)(medlev medu midnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
*4) Imposing restrictions
*i) Dropping children whose parents are unknown
drop if missing(fidnr) & missing(midnr)
*ii) Dropping parental income averages with less than 2 observed incomes
replace fearn=. if fnearn<2
replace mearn=. if mnearn<2
*iii) Dropping children without parental income
drop if fearn==. & mearn==.
*5a) Generating combined parental income, employment and education measures
egen fmearn=rowmean(fearn mearn)
egen fmmax=rowmax(fearn mearn)
egen fmedu=rowmean(fedu medu)
egen fmemp=rowmean(femp memp)
lab var fmearn "average parental earnings"
lab var fmmax "max of parental earnings"
lab var fmedu "average parental education"
lab var fmemp "average parental employment"
*5b) Ranks for child and average parental income by (childs') cohort separately by gender (of child)
local vars "fmearn fmmax earn fearn mearn"
foreach x of local vars{
	bysort yob woman: egen n=count(`x')
	bysort yob woman: egen i=rank(`x')
	bysort yob woman: g p`x'=(i-1)/(n-1)*100
	drop n i
}
lab var pearn "earnings rank by yob and gender"
lab var pfearn "father's earnings rank by child's yob and gender"
lab var pmearn "mother's earnings rank by child's yob and gender"
lab var pfmearn "parental earnings rank by child's yob and gender"
lab var pfmmax "parental earnings rank of max average"
g agec1=year-yob-40
g agef1=fobsy-fyob
g agem1=mobsy-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
*Sons on average parental income
global fig "a5s"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on average parental income
global fig "a5d"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on fathers' income
global fig "a5f"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on mothers' income
global fig "a5m"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
set obs 35
g year=_n+1984
local est "a5s a5d a5f a5m"
foreach e of local est{
	svmat b`e'
	svmat se`e'
	svmat var`e'
	g sd`e'u=b`e'1+1.96*se`e'1
	g sd`e'l=b`e'1-1.96*se`e'1
}
rename (ba5s1 ba5d1 ba5f1 ba5m1) (roba5s roba5d roba5f roba5m)
lab var roba5s "son/parental average"
lab var roba5d "daughter/parental average"
lab var roba5f "pooled/father"
lab var roba5m "pooled/mother"
lab var sda5su "son/parental average"
lab var sda5du "daughter/parental average"
lab var sda5fu "pooled/father"
lab var sda5mu "pooled/mother"
lab var sda5sl "son/parental average"
lab var sda5dl "daughter/parental average"
lab var sda5fl "pooled/father"
lab var sda5ml "pooled/mother"
compress
save "use\figa5_swe", replace
*Figure A5a: sons and daughters on combined parental income
twoway (connect roba5s year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda5su sda5sl year, lc(ebblue)) (connect roba5d year, ms(d) lc(cranberry) mc(cranberry)) (rspike sda5du sda5dl year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a5a", replace)
graph export "graphs\us_swe\a5a.pdf", replace
graph export "graphs\us_swe\a5a.eps", replace
*Figure A5b: sons and daughters pooled on fathers' and mothers' income
twoway (connect roba5f year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda5fu sda5fl year, lc(ebblue)) (connect roba5m year, ms(d) lc(cranberry) mc(cranberry)) (rspike sda5mu sda5ml year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Fathers") label(3 "Mothers")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a5b", replace)
graph export "graphs\us_swe\a5b.pdf", replace
graph export "graphs\us_swe\a5b.eps", replace
********************************************************************************
*Figure A6: Trends for different child age intervalls
*29-31 years of age
use if yob>=1952 & yob<=1994 & age>=29 & age<=31 & earn!=. using "use\incpanel2", replace
*1) Adding parents
sort idnr
merge m:1 idnr using "use\multigen", keepusing(midnr fidnr myob fyob bcountry mbcountry fbcountry) keep(1 3) nogen
*2) Adding parental earnings and employment 
*mothers
rename (idnr midnr earn emp)(temp1 idnr temp2 temp3)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(mearn mnearn mobsy memp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace mearn=earn if yob==19`i'
	replace mnearn=nearn if yob==19`i'
	replace mobsy=obsy if yob==19`i'
	replace memp=emp if yob==19`i'
	drop earn nearn obsy emp
}
*fathers
rename (idnr fidnr)(midnr idnr)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(fearn fnearn fobsy femp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace fearn=earn if yob==19`i'
	replace fnearn=nearn if yob==19`i'
	replace fobsy=obsy if yob==19`i'
	replace femp=emp if yob==19`i'
	drop earn nearn obsy emp
}
rename (idnr temp1 temp2 temp3)(fidnr idnr earn emp)
mvencode fnearn mnearn, mv(0)
lab var fearn "father's earnings"
lab var mearn "mother's earnings"
lab var fnearn "observations father's earnings"
lab var mnearn "observations mother's earnings"
lab var fobsy "year father's earnings observed"
lab var mobsy "year mother's earnings observed"
lab var femp "father's employment"
lab var memp "mother's employment"
*3) Adding education
rename(idnr fidnr)(temp idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr midnr)(fedlev fedu fidnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr temp)(medlev medu midnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
*4) Imposing restrictions
*i) Dropping children whose parents are unknown
drop if missing(fidnr) & missing(midnr)
*ii) Dropping parental income averages with less than 2 observed incomes
replace fearn=. if fnearn<2
replace mearn=. if mnearn<2
*iii) Dropping children without parental income
drop if fearn==. & mearn==.
*5a) Generating combined parental income, employment and education measures
egen fmearn=rowmean(fearn mearn)
egen fmmax=rowmax(fearn mearn)
egen fmedu=rowmean(fedu medu)
egen fmemp=rowmean(femp memp)
lab var fmearn "average parental earnings"
lab var fmmax "max of parental earnings"
lab var fmedu "average parental education"
lab var fmemp "average parental employment"
*5b) Ranks for child and average parental earnings by (child's) cohort separately by gender (of child)
local vars "fmearn fmmax earn fearn mearn"
foreach x of local vars{
	bysort yob woman: egen n=count(`x')
	bysort yob woman: egen i=rank(`x')
	bysort yob woman: g p`x'=(i-1)/(n-1)*100
	drop n i
}
lab var pearn "earnings rank by yob and gender"
lab var pfearn "father's earnings rank by child's yob and gender"
lab var pmearn "mother's earnings rank by child's yob and gender"
lab var pfmearn "parental earnings rank by child's yob and gender"
lab var pfmmax "parental earnings rank of max average"
compress
sort idnr year
save "use\samp2931", replace
*34-36 years of age
use if yob>=1952 & yob<=1994 & age>=34 & age<=36 & earn!=. using "use\incpanel2", replace
*1) Adding parents
sort idnr
merge m:1 idnr using "use\multigen", keepusing(midnr fidnr myob fyob bcountry mbcountry fbcountry) keep(1 3) nogen
*2) Adding parental earnings and employment 
*mothers
rename (idnr midnr earn emp)(temp1 idnr temp2 temp3)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(mearn mnearn mobsy memp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace mearn=earn if yob==19`i'
	replace mnearn=nearn if yob==19`i'
	replace mobsy=obsy if yob==19`i'
	replace memp=emp if yob==19`i'
	drop earn nearn obsy emp
}
*fathers
rename (idnr fidnr)(midnr idnr)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(fearn fnearn fobsy femp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace fearn=earn if yob==19`i'
	replace fnearn=nearn if yob==19`i'
	replace fobsy=obsy if yob==19`i'
	replace femp=emp if yob==19`i'
	drop earn nearn obsy emp
}
rename (idnr temp1 temp2 temp3)(fidnr idnr earn emp)
mvencode fnearn mnearn, mv(0)
lab var fearn "father's earnings"
lab var mearn "mother's earnings"
lab var fnearn "observations father's earnings"
lab var mnearn "observations mother's earnings"
lab var fobsy "year father's earnings observed"
lab var mobsy "year mother's earnings observed"
lab var femp "father's employment"
lab var memp "mother's employment"
*3) Adding education
rename(idnr fidnr)(temp idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr midnr)(fedlev fedu fidnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr temp)(medlev medu midnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
*4) Imposing restrictions
*i) Dropping children whose parents are unknown
drop if missing(fidnr) & missing(midnr)
*ii) Dropping parental income averages with less than 2 observed incomes
replace fearn=. if fnearn<2
replace mearn=. if mnearn<2
*iii) Dropping children without parental income
drop if fearn==. & mearn==.
*5a) Generating combined parental income, employment and education measures
egen fmearn=rowmean(fearn mearn)
egen fmmax=rowmax(fearn mearn)
egen fmedu=rowmean(fedu medu)
egen fmemp=rowmean(femp memp)
lab var fmearn "average parental earnings"
lab var fmmax "max of parental earnings"
lab var fmedu "average parental education"
lab var fmemp "average parental employment"
*5b) Ranks for child and average parental earnings by (child's) cohort separately by gender (of child)
local vars "fmearn fmmax earn fearn mearn"
foreach x of local vars{
	bysort yob woman: egen n=count(`x')
	bysort yob woman: egen i=rank(`x')
	bysort yob woman: g p`x'=(i-1)/(n-1)*100
	drop n i
}
lab var pearn "earnings rank by yob and gender"
lab var pfearn "father's earnings rank by child's yob and gender"
lab var pmearn "mother's earnings rank by child's yob and gender"
lab var pfmearn "parental earnings rank by child's yob and gender"
lab var pfmmax "parental earnings rank of max average"
compress
sort idnr year
save "use\samp3436", replace
*39-41 years of age
use if yob>=1952 & yob<=1994 & age>=39 & age<=41 & earn!=. using "use\incpanel2", replace
*1) Adding parents
sort idnr
merge m:1 idnr using "use\multigen", keepusing(midnr fidnr myob fyob bcountry mbcountry fbcountry) keep(1 3) nogen
*2) Adding parental earnings and employment 
*mothers
rename (idnr midnr earn emp)(temp1 idnr temp2 temp3)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(mearn mnearn mobsy memp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace mearn=earn if yob==19`i'
	replace mnearn=nearn if yob==19`i'
	replace mobsy=obsy if yob==19`i'
	replace memp=emp if yob==19`i'
	drop earn nearn obsy emp
}
*fathers
rename (idnr fidnr)(midnr idnr)
sort idnr
merge m:1 idnr using "use\earn52_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
replace earn=. if yob!=1952
replace nearn=. if yob!=1952
replace obsy=. if yob!=1952
replace emp=. if yob!=1952
rename (earn nearn obsy emp)(fearn fnearn fobsy femp)
forval i=53/94{
	merge m:1 idnr using "use\earn`i'_2", keepusing(earn nearn obsy emp) nogen keep(1 3)
	replace fearn=earn if yob==19`i'
	replace fnearn=nearn if yob==19`i'
	replace fobsy=obsy if yob==19`i'
	replace femp=emp if yob==19`i'
	drop earn nearn obsy emp
}
rename (idnr temp1 temp2 temp3)(fidnr idnr earn emp)
mvencode fnearn mnearn, mv(0)
lab var fearn "father's earnings"
lab var mearn "mother's earnings"
lab var fnearn "observations father's earnings"
lab var mnearn "observations mother's earnings"
lab var fobsy "year father's earnings observed"
lab var mobsy "year mother's earnings observed"
lab var femp "father's employment"
lab var memp "mother's employment"
*3) Adding education
rename(idnr fidnr)(temp idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr midnr)(fedlev fedu fidnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
rename(edlev edu idnr temp)(medlev medu midnr idnr)
sort idnr
merge m:1 idnr using "use\edu_max", keepusing(edlev edu) keep(1 3) nogen
*4) Imposing restrictions
*i) Dropping children whose parents are unknown
drop if missing(fidnr) & missing(midnr)
*ii) Dropping parental income averages with less than 2 observed incomes
replace fearn=. if fnearn<2
replace mearn=. if mnearn<2
*iii) Dropping children without parental income
drop if fearn==. & mearn==.
*5a) Generating combined parental income, employment and education measures
egen fmearn=rowmean(fearn mearn)
egen fmmax=rowmax(fearn mearn)
egen fmedu=rowmean(fedu medu)
egen fmemp=rowmean(femp memp)
lab var fmearn "average parental earnings"
lab var fmmax "max of parental earnings"
lab var fmedu "average parental education"
lab var fmemp "average parental employment"
*5b) Ranks for child and average parental earnings by (child's) cohort separately by gender (of child)
local vars "fmearn fmmax earn fearn mearn"
foreach x of local vars{
	bysort yob woman: egen n=count(`x')
	bysort yob woman: egen i=rank(`x')
	bysort yob woman: g p`x'=(i-1)/(n-1)*100
	drop n i
}
lab var pearn "earnings rank by yob and gender"
lab var pfearn "father's earnings rank by child's yob and gender"
lab var pmearn "mother's earnings rank by child's yob and gender"
lab var pfmearn "parental earnings rank by child's yob and gender"
lab var pfmmax "parental earnings rank of max average"
compress
sort idnr year
save "use\samp3941", replace
*IRP estimations
local list "2931 3436 3941"
foreach y of local list{
use if year>=1985 using "use\samp`y'", replace
g agef1=fobsy-fyob
g agem1=mobsy-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
*Sons on average parental income
global fig "a6s"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* if woman==0, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on average parental income
global fig "a6d"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* if woman==1, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on fathers' income
global fig "a6f"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep*, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on mothers' income
global fig "a6m"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep*, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
if (`y'==2931){
    set obs 35
	g year=_n+1984
}
else if(`y'==3436){
    set obs 34
	g year=_n+1985
}
else if(`y'==3941){
    set obs 29
	g year=_n+1990
}
local est "a6s a6d a6f a6m"
foreach e of local est{
	svmat b`e'
	svmat se`e'
	svmat var`e'
	g sd`e'u=b`e'1+1.96*se`e'1
	g sd`e'l=b`e'1-1.96*se`e'1
}
rename (ba6s1 ba6d1 ba6f1 ba6m1) (roba6s roba6d roba6f roba6m)
lab var roba6s "son/parental average"
lab var roba6d "daughter/parental average"
lab var roba6f "pooled/father"
lab var roba6m "pooled/mother"
lab var sda6su "son/parental average"
lab var sda6du "daughter/parental average"
lab var sda6fu "pooled/father"
lab var sda6mu "pooled/mother"
lab var sda6sl "son/parental average"
lab var sda6dl "daughter/parental average"
lab var sda6fl "pooled/father"
lab var sda6ml "pooled/mother"
compress
sort year
save "temp\temp`y'", replace
}
*29-31 years of age
use year roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml using "temp\temp2931", replace
rename (roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml) (roba6s_2931 roba6d_2931 roba6f_2931 roba6m_2931 sda6su_2931 sda6du_2931 sda6fu_2931 sda6mu_2931 sda6sl_2931 sda6dl_2931 sda6fl_2931 sda6ml_2931)
*34-36 years of age
merge 1:1 year using "temp\temp3436", keepusing(roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml) nogen
rename (roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml) (roba6s_3436 roba6d_3436 roba6f_3436 roba6m_3436 sda6su_3436 sda6du_3436 sda6fu_3436 sda6mu_3436 sda6sl_3436 sda6dl_3436 sda6fl_3436 sda6ml_3436)
*39-41 years of age
merge 1:1 year using "temp\temp3941", keepusing(roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml) nogen
rename (roba6s roba6d roba6f roba6m sda6su sda6du sda6fu sda6mu sda6sl sda6dl sda6fl sda6ml) (roba6s_3941 roba6d_3941 roba6f_3941 roba6m_3941 sda6su_3941 sda6du_3941 sda6fu_3941 sda6mu_3941 sda6sl_3941 sda6dl_3941 sda6fl_3941 sda6ml_3941)
sort year
compress
save "use\figa6_swe", replace
*Figure A6a: sons on average parental income
twoway (connect roba6s_2931 year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda6su_2931 sda6sl_2931 year, lc(ebblue)) (connect roba6s_3436 year, ms(d) lc(blue) mc(blue)) (rspike sda6su_3436 sda6sl_3436 year, lc(blue)) (connect roba6s_3941 year, ms(t) lc(navy) mc(navy)) (rspike sda6su_3941 sda6sl_3941 year, lc(navy)), ytitle("Rank persistence") xtitle("Year")  graphr(fc(white) c(white)) legend(order(1 3 5) hole(2 4) label(1 "Aged 29-31") label(3 "Aged 34-36") label(5 "Aged 39-41")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a6a", replace)
graph export "graphs\us_swe\a6a.eps", replace
graph export "graphs\us_swe\a6a.pdf", replace
*Figure A6b: daughters on average parental income
twoway (connect roba6d_2931 year, ms(o) lc(cranberry) mc(cranberry)) (rspike sda6du_2931 sda6dl_2931 year, lc(cranberry)) (connect roba6d_3436 year, ms(d) lc(red) mc(red)) (rspike sda6du_3436 sda6dl_3436 year, lc(red)) (connect roba6d_3941 year, ms(t) lc(orange) mc(orange)) (rspike sda6du_3941 sda6dl_3941 year, lc(orange)), ytitle("Rank persistence") xtitle("Year")  graphr(fc(white) c(white)) legend(order(1 3 5) hole(2 4) label(1 "Aged 29-31") label(3 "Aged 34-36") label(5 "Aged 39-41")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a6b", replace)
graph export "graphs\us_swe\a6b.eps", replace
graph export "graphs\us_swe\a6b.pdf", replace
*Figure A6c: sons and daughters pooled on fathers' income
twoway (connect roba6f_2931 year, ms(o) lc(ebblue) mc(ebblue)) (rspike sda6fu_2931 sda6fl_2931 year, lc(ebblue)) (connect roba6f_3436 year, ms(d) lc(blue) mc(blue)) (rspike sda6fu_3436 sda6fl_3436 year, lc(blue)) (connect roba6f_3941 year, ms(t) lc(navy) mc(navy)) (rspike sda6fu_3941 sda6fl_3941 year, lc(navy)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3 5) hole(2 4) label(1 "Aged 29-31") label(3 "Aged 34-36") label(5 "Aged 39-41")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a6c", replace)
graph export "graphs\us_swe\a6c.eps", replace
graph export "graphs\us_swe\a6c.pdf", replace
*Figure A6d: sons and daughters pooled on mothers' income
twoway (connect roba6m_2931 year, ms(o) lc(cranberry) mc(cranberry)) (rspike sda6mu_2931 sda6ml_2931 year, lc(cranberry)) (connect roba6m_3436 year, ms(d) lc(red) mc(red)) (rspike sda6mu_3436 sda6ml_3436 year, lc(red)) (connect roba6m_3941 year, ms(t) lc(orange) mc(orange)) (rspike sda6mu_3941 sda6ml_3941 year, lc(orange)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3 5) hole(2 4) label(1 "Aged 29-31") label(3 "Aged 34-36") label(5 "Aged 39-41")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\a6d", replace)
graph export "graphs\us_swe\a6d.eps", replace
graph export "graphs\us_swe\a6d.pdf", replace
********************************************************************************
*Figure A7: Trends across five-year cohort groups
use *idnr *earn* *yob *obsy *max* year woman if year>=1985 using "use\swepanel_zeros", replace
*5-year cohorts
gen cohort=.
replace cohort=1 if (yob>=1952 & yob<=1956)
replace cohort=2 if (yob>=1957 & yob<=1961)
replace cohort=3 if (yob>=1962 & yob<=1966)
replace cohort=4 if (yob>=1967 & yob<=1971)
replace cohort=5 if (yob>=1972 & yob<=1976)
replace cohort=6 if (yob>=1977 & yob<=1981)
replace cohort=7 if (yob>=1982 & yob<=1986)
replace cohort=8 if (yob>=1987 & yob<=1993)
*Age controls
g agec1=year-yob-40
g agef1=fobsy-fyob
g agem1=mobsy-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
*Sons on average parental income
global fig "a7s"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
	sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on average parental income
global fig "a7d"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
	sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on fathers' income
global fig "a7f"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
    sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons and daughters pooled on mothers' income
global fig "a7m"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
    sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons on fathers' income
global fig "a7sf"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
    sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on  fathers' income
global fig "a7df"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
    sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Sons on mothers' income
global fig "a7sm"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof cohort, local(cohorts)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
    sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Daughters on mothers' income
global fig "a7dm"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.cohort#c.$incp i.cohort $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof cohort, local(cohort)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local cohorts{
	sca t1=_se[`i'.cohort#c.$incp]
	mat $se=$se\t1
	sum $inc if cohort==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if cohort==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
set obs 8
g cohort=_n
lab def t1 1 "1952-1956" 2 "1957-1961" 3 "1962-1966" 4 "1967-1971" 5 "1972-1976" 6 "1977-1981" 7 "1982-1986" 8 "1987-1993"
lab val cohort t1
local est "a7s a7d a7f a7m a7sf a7df a7sm a7dm"
foreach e of local est{
	svmat b`e'
	svmat se`e'
	svmat var`e'
	g sd`e'u=b`e'1+1.96*se`e'1
	g sd`e'l=b`e'1-1.96*se`e'1
}
rename (ba7s1 ba7d1 ba7f1 ba7m1 ba7sf1 ba7df1 ba7sm1 ba7dm1) (roba7s roba7d roba7f roba7m roba7sf roba7df roba7sm roba7dm)
compress
save "use\figa7_swe", replace
*Figure A7a: sons and daughters separately on average parental income
twoway (connect roba7s cohort, ms(o) lc(ebblue) mc(ebblue)) (rspike sda7su sda7sl cohort, lc(ebblue)) (connect roba7d cohort, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sda7du sda7dl cohort, lc(cranberry)), ytitle("Rank persistence") xtitle("Cohorts") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1 8)) xlab(1 "52-56" 2 "57-61" 3 "62-66" 4 "67-71" 5 "72-76" 6 "77-81" 7 "82-86" 8 "87-93") saving("graphs\us_swe\a7a", replace)
graph export "graphs\us_swe\a7a.eps", replace
graph export "graphs\us_swe\a7a.pdf", replace
*Figure A7b: sons and daughters pooled on fathers' and mothers' income
twoway (connect roba7f cohort, ms(o) lc(ebblue) mc(ebblue)) (rspike sda7fu sda7fl cohort, lc(ebblue)) (connect roba7m cohort, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sda7mu sda7ml cohort, lc(cranberry)), ytitle("Rank persistence") xtitle("Cohorts") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Fathers") label(3 "Mothers")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1 8)) xlab(1 "52-56" 2 "57-61" 3 "62-66" 4 "67-71" 5 "72-76" 6 "77-81" 7 "82-86" 8 "87-93") saving("graphs\us_swe\a7b", replace)
graph export "graphs\us_swe\a7b.eps", replace
graph export "graphs\us_swe\a7b.pdf", replace
*Figure A7c: sons and daughters separately on fathers' income
twoway (connect roba7sf cohort, ms(o) lc(ebblue) mc(ebblue)) (rspike sda7sfu sda7sfl cohort, lc(ebblue)) (connect roba7df cohort, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sda7dfu sda7dfl cohort, lc(cranberry)), ytitle("Rank persistence") xtitle("Cohorts") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1 8)) xlab(1 "52-56" 2 "57-61" 3 "62-66" 4 "67-71" 5 "72-76" 6 "77-81" 7 "82-86" 8 "87-93") saving("graphs\us_swe\a7c", replace)
graph export "graphs\us_swe\a7c.eps", replace
graph export "graphs\us_swe\a7c.pdf", replace
*Figure A7d: sons and daughters separately on mother's income
twoway (connect roba7sm cohort, ms(o) lc(ebblue) mc(ebblue)) (rspike sda7smu sda7sml cohort, lc(ebblue)) (connect roba7dm cohort, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sda7dmu sda7dml cohort, lc(cranberry)), ytitle("Rank persistence") xtitle("Cohorts") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1 8)) xlab(1 "52-56" 2 "57-61" 3 "62-66" 4 "67-71" 5 "72-76" 6 "77-81" 7 "82-86" 8 "87-93") saving("graphs\us_swe\a7d", replace)
graph export "graphs\us_swe\a7d.eps", replace
graph export "graphs\us_swe\a7d.pdf", replace
********************************************************************************
*Figure A.9: IGE trend for sons and daughters pooled on average parental income
use *idnr *earn* *yob *obsy* *max* year woman if year>=1985 using "use\swepanel_100usd", replace
g agec1=year-yob-40
g agef1=fobsy_c-fyob
g agem1=mobsy_c-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}
*Sons and daughters pooled on combined parental income
global inc "learn_c"
global incp "lfmearn_c"
global agep "fmage"
global beta "a" /*matrix to store estimates*/
global se "se" /*matrix to store standard errors*/
global var "var" /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
set obs 35
g year=_n+1984

svmat a
svmat se
svmat var
g sdu=a1+1.96*se1
g sdl=a1-1.96*se1
rename (a1) (ige)
lab var ige "IGE pooled/parental average"
compress
save "use\figa9_swe", replace
*Figure A9: sons and daughters pooled on average parental income
twoway (connect ige year, ms(o) lc(ebblue) mc(ebblue)) (rspike sdu sdl year, lc(ebblue)), /*
*/title("IGE") subtitle("Parental average vs sons and daughters pooled") ytitle("Elasticity") xtitle("Year") /*
*/graphr(fc(white) c(white)) legend(order(1) label(1 "IGE parental average")) /*
*/ysc(r(0 0.3)) ylab(0(0.05)0.3) xsc(r(1985 2020)) xlab(1985(5)2020) /*
*/saving("graphs\us_swe\a9", replace)
graph export "graphs\us_swe\a9.pdf", replace
graph export "graphs\us_swe\a9.eps", replace